package com.zhaopin.thread;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author yin.peide
 * @date 2021-06-23 9:59
 */
public class LockTest {

    public static void main(String[] args) {

        LockTest room = new LockTest();

        AtomicBoolean flag = new AtomicBoolean(false);
        Thread t1 = new Thread(() -> {
            synchronized (room){
                while (!flag.get()){
                    try {
                        room.wait();

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        System.out.println(Thread.currentThread().getName() + Thread.currentThread().isInterrupted());
                    }
                }

                if(flag.get()){
                    System.out.println("可以开始干活了");
                }
            }
        },"t1");
        t1.start();

        new Thread(() -> {
            synchronized (room){
                try {
                    System.out.println("等待一秒");
                    Thread.sleep(1000);
                    System.out.println("等待完了");

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                room.notifyAll();
                flag.set(true);
            }
        },"t2").start();



    }

}
